<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class VoucherLunisolar_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_voucher';
	}
        
        public function updateResidual($params){
            $sql = "UPDATE {$this->table_name} SET residual = 0 WHERE customerID = '{$params->customerID}' AND phone='{$params->phone}' AND owner='{$params->owner}'";
            $this->db->query($sql);
            return $this->db->affected_rows();
        }
        
        public function getSurByUserid($customerID){
                $this->db->select('customerID, residual');
                $this->db->from($this->table_name);
                $this->db->where('residual > 0');
                $this->db->where('checkbox_sodukh',1);
                $this->db->where('customerID',$customerID);
                $query = $this->db->get();
//                echo $this->db->last_query();exit;
                return $query->result();
        }

        public function getListBookingTypes(){
            return $this->booking_types;
        }

        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '15%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'residual',
						'label' => $this->lang->line('residual'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'action',
						'label' => $this->lang->line('action'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				)
		);
	}
	
	public function json_data($controller, $right){
            
                if($this->session->userdata('v_voucher')){
                    $arr_status = json_decode($this->session->userdata('v_voucher'));
                }
            
		$this->datatables
		->select("tbl.id,tbl.*,bc.name,usr.username")
                ->from($this->table_name.' AS tbl')
                //->join('book_payment_method AS method','method.id = tbl.payment_method','LEFT')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID')
                ->join('users AS usr','usr.id=tbl.owner')    
                ->where('tbl.residual != 0')->where('tbl.checkbox_sodukh',1);
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                
                $this->session->unset_userdata('v_voucher');
                
		foreach($datatables['aaData'] as $item){
                        if($item['booking_place_code'] != "" && $item['residual'] < 0){
                            continue;
                        }
                        $booking_place_code = json_decode($item['booking_place_code']);
                        $airline            = json_decode($item['airline']);
                        
                        $pnr                            = '';
                        foreach($booking_place_code as $key => $vals){
                            if($key < count($booking_place_code)-1){
                                $etc = ', ';
                            } else {
                                $etc = '';
                            }
                            $pnr .= $vals.'-'.$airline[$key].$etc;
                        }
                        
//                        if($item['payment_method']==4){
//                            $approve = ($item['approve']==1)?$this->lang->line('approved'):$this->lang->line('unapproved');
//                        } else {
//                            $approve = '';
//                        }
                        
			$ouput['aaData'][] = array(
					$item['id'],
                                        $item['name'],
                                        ($pnr)?$pnr:'',
                                        $this->bookinglib->my_number_format($item['residual'],2, ',', ','),
                                       // $item['payment_name'],
                                        $item['username'],
                                        date("d-m-Y H:i:s",strtotime($item['lastupdated'])),
                                        $this->add_button_public($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}

}